package stru1.day9;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * 232. 用栈实现队列
 */
public class MyQueue {
    private final Deque<Integer> s1; //实际存储元素
    private final Deque<Integer> s2; //用来反转顺序
    private int front; //记录队头

    public MyQueue() {
        s1 = new ArrayDeque<>();
        s2 = new ArrayDeque<>();
    }

    public void push(int x) {
        if (s1.isEmpty())
            front = x;
        while (!s1.isEmpty()) s2.push(s1.pop());
        s2.push(x);
        while (!s2.isEmpty()) s1.push(s2.pop());
    }

    public int pop() {
        int pop = s1.pop();
        if (!s1.isEmpty()) front = s1.peek();
        return pop;
    }

    public int peek() {
        return front;
    }

    public boolean empty() {
        return s1.isEmpty();
    }
}
